package com.sfjswl.fp.core.handler;

import com.sfjswl.fp.common.enums.TimeTypeEnum;
import com.sfjswl.fp.common.utils.JacksonUtils;
import com.sfjswl.fp.core.base.BaseNode;
import com.sfjswl.fp.core.context.RuleContext;
import com.sfjswl.fp.core.utils.RuleTimeUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Set;

@Slf4j
@Data
public class RuleHandler {


  private long ruleId;

  private long confId;

  private Set<String> scenes;

  private TimeTypeEnum timeTypeEnum;

  private long start;

  private long end;

  private BaseNode root;

  public void handle(RuleContext ctx) {
    if (RuleTimeUtils.timeDisable(timeTypeEnum, ctx.getPack().getRequestTime(), start, end)) {
      return;
    }
    try {
      if (root != null) {
        root.process(ctx);
      } else {
        log.error("root not exist please check! ruleId:{}", this.ruleId);
      }
    } catch (Throwable t) {
      log.error("error ruleId:{} ctx:{}", this.ruleId, JacksonUtils.toJsonString(ctx), t);
    }
  }
}
